﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Omu.AwesomeWebForms;

namespace VerySimple
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Aja
    {
        #region sample data (fruits and categories)
        public static readonly IList<Category> Categories = new List<Category>
         {
             new Category{Id=1, Name = "Fruits"},
             new Category{Id=2, Name = "Legumes"},
             new Category{Id=3, Name = "Vegetables"},
             new Category{Id=4, Name = "Nuts"},
             new Category{Id=5, Name = "Grains"},
         };

        public static readonly IList<Fruit> Fruits = new List<Fruit>
                                                 {
                                                     new Fruit{Id=1, Name = "Mango", Category = Categories[0]},
                                                     new Fruit{Id=2, Name = "Apple", Category = Categories[0]},
                                                     new Fruit{Id=3, Name = "Papaya", Category = Categories[0]},
                                                     new Fruit{Id=4, Name = "Banana", Category = Categories[0]},
                                                     new Fruit{Id=5, Name = "Cherry", Category = Categories[0]},

                                                     new Fruit{Id=10, Name = "Tomato", Category = Categories[1]},
                                                     new Fruit{Id=11, Name = "Potato", Category = Categories[1]},
                                                     new Fruit{Id=12, Name = "Cucumber", Category = Categories[1]},
                                                     new Fruit{Id=13, Name = "Onion", Category = Categories[1]},
                                                     new Fruit{Id=14, Name = "Carrot", Category = Categories[1]},
                                                     
                                                     new Fruit{Id=20, Name = "Celery", Category = Categories[2]},
                                                     new Fruit{Id=21, Name = "Broccoli", Category = Categories[2]},
                                                     new Fruit{Id=22, Name = "Artichoke", Category = Categories[2]},
                                                     new Fruit{Id=23, Name = "Cauliflower", Category = Categories[2]},
                                                     new Fruit{Id=24, Name = "Lettuce", Category = Categories[2]},
                                                     
                                                     new Fruit{Id=30, Name = "Hazelnuts", Category = Categories[3]},
                                                     new Fruit{Id=31, Name = "Chestnuts", Category = Categories[3]},
                                                     new Fruit{Id=32, Name = "Walnut", Category = Categories[3]},
                                                     new Fruit{Id=33, Name = "Almonds", Category = Categories[3]},
                                                     new Fruit{Id=34, Name = "Mongongo", Category = Categories[3]},
                                                     
                                                     new Fruit{Id=40, Name = "Rice", Category = Categories[4]},
                                                     new Fruit{Id=41, Name = "Wheat", Category = Categories[4]},
                                                     new Fruit{Id=42, Name = "Oats", Category = Categories[4]},
                                                     new Fruit{Id=43, Name = "Fonio", Category = Categories[4]},
                                                     new Fruit{Id=44, Name = "Barley", Category = Categories[4]},
                                                 }; 
        #endregion

        [WebGet]
        [OperationContract]
        public IEnumerable<SelectListItem> CategoriesDropdown(string v)
        {
            return Categories.Select(o => new SelectListItem(o.Id, o.Name, v == o.Id.ToString()));
        }

        #region FruitLookup
        [WebGet]
        [OperationContract]
        public KeyContent FruitGet(string v)
        {
            var f = Fruits.Single(o => o.Id.ToString() == v);
            return new KeyContent(f.Id, f.Name);
        }

        [WebGet]
        [OperationContract]
        public LookupResult FruitSearch(string search)
        {
            return new LookupResult
                       {
                           Items = Fruits.Where(o => o.Name.ToLower().Contains(search.ToLower()))
                                         .Select(o => new KeyContent(o.Id, o.Name))
                       };
        } 
        #endregion

        #region Fruit MultiLookup
        [WebGet]
        [OperationContract]
        public IEnumerable<KeyContent> FruitGetMultiple(string v)
        {
            var ids = v.GetIntArray();
            return Fruits.Where(o => ids.Contains(o.Id))
                         .Select(f => new KeyContent(f.Id, f.Name));
        }

        [WebGet]
        [OperationContract]
        public LookupResult FruitSearchML(string search, string selected)
        {
            var sel = selected.GetIntArray();

            var items = Fruits.Where(o => o.Name.ToLower().Contains(search) && !sel.Contains(o.Id))
                .Select(f => new KeyContent { Key = f.Id, Content = f.Name });

            return new LookupResult
            {
                Items = items
            };
        }

        [WebGet]
        [OperationContract]
        public IEnumerable<KeyContent> FruitSelected(string selected)
        {
            var sel = selected.GetIntArray();
            return Fruits.Where(o => sel.Contains(o.Id))
                .Select(f => new KeyContent(f.Id, f.Name));
        } 
        #endregion

        [WebGet]
        [OperationContract]
        public IEnumerable<SelectListItem> CategoriesCheckboxList(string v)
        {
            var keys = v.GetIntArray();
            return Categories.Select(o => new SelectListItem(o.Id,o.Name,keys.Contains(o.Id)));
        }

        [WebGet]
        [OperationContract]
        public LookupResult FruitListSearch(int page, bool isMoreClick)
        {
            var items = Fruits.Select(f => new KeyContent { Key = f.Id, Content = f.Name });

            const int pageSize = 5;
            IEnumerable<KeyContent> Items;
            
            if (!isMoreClick && page > 1)
                //if on page load page > 1 
                Items = items.Take(page * pageSize);//return all data up to this page
            else
                Items = items.Skip((page - 1) * pageSize).Take(pageSize);

            return new LookupResult
            {
                Items = Items,
                More = items.Count() > page * pageSize
            };
        }
    }

    public class Fruit
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Category Category { get; set; }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
